package com.xcc.web;/**
 * ClassName:LoginServlet
 * package:com.xcc.web
 * Description: TODO
 *
 * @date ：2019/7/3 20:26
 * @Author : 安儿
 */

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @create: 2019-07-03 20:26
 * @author: Aner
 * @description:
 **/
@WebServlet(name="loginServlet",urlPatterns = "/login")
public class LoginServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            String error =null;
            String username=req.getParameter("username");
            String password=req.getParameter("password");

            Subject subject =SecurityUtils.getSubject();

            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
       try{
           subject.login(token);
       }catch (UnknownAccountException  e){
           error ="用户/密码错误";
       }catch (IncorrectCredentialsException  e){
           error ="用户/密码错误";
       }catch (AuthenticationException e){
           error ="其他错误"+e.getMessage();
       }
        if (error != null) { //出错了，返回登录页面
                req.setAttribute("error",error);
                req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req,resp);
        }else{ //登录成功
            req.setAttribute("subject",subject);
            req.getRequestDispatcher("/WEB-INF/jsp/loginSuccess.jsp").forward(req,resp);
        }
    }
}
